package cn.edu.henu.team.ui_test;

import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import androidx.appcompat.app.AlertDialog;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean.descAnalyse;
import cn.edu.henu.team.bean.message;

public class DescribeActivity extends BaseActivity{
    private String Saying_content;
    private ImageView back;
    private EditText Saying_content_edittext;
    private ScrollView mScrollView;
    private TextView confirm,text1,text2,text3,text4,text5,text6,textRight,result,kind1,kind2,kind3;

    private String[] str;  // 接收分割后的字符串
    private Set<String> descResult = new HashSet<String>();
    private Set<String> kindSet = new HashSet<String>();

    @Override
    protected int initLayout() {
        return R.layout.activity_describe;
    }

    @Override
    protected void initView() {
        back = findViewById(R.id.back);
        confirm = findViewById(R.id.confirm);
        text1 = findViewById(R.id.text1);
        text2 = findViewById(R.id.text2);
        text3 = findViewById(R.id.text3);
        text4 = findViewById(R.id.text4);
        text5 = findViewById(R.id.text5);
        text6 = findViewById(R.id.text6);
        result = findViewById(R.id.result);
        kind1 = findViewById(R.id.kind1);
        kind2 = findViewById(R.id.kind2);
        kind3 = findViewById(R.id.kind3);
        textRight = findViewById(R.id.textRight);
        mScrollView = findViewById(R.id.scrollView);
        Saying_content_edittext = findViewById(R.id.Saying_content);
        Saying_content_edittext.addTextChangedListener(new TextWatcher() {
            //每次EditText文本改变之前的时候，会回调这个方法
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            //每次EditText文本改变的时候，会回调这个方法
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //第一个参数s 的含义: 输入框中改变后的字符串信息
                Saying_content = s.toString();
                text1.setVisibility(View.GONE);
                text2.setVisibility(View.GONE);
                text3.setVisibility(View.GONE);
                text4.setVisibility(View.GONE);
                text5.setVisibility(View.GONE);
                text6.setVisibility(View.GONE);
                kind1.setVisibility(View.GONE);
                kind2.setVisibility(View.GONE);
                kind3.setVisibility(View.GONE);
                result.setVisibility(View.GONE);
                mScrollView.setVisibility(View.GONE);
                textRight.setVisibility(View.GONE);
            }

            //每次EditText文本改变之后的时候，会回调这个方法
            @Override
            public void afterTextChanged(Editable s) {
                //edit  输入结束呈现在输入框中的信息
            }
        });
    }

    @Override
    protected void initData() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Saying_content == null)
                    finish();
                else {
                    createDialog();
                }
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Saying_content.equals(""))
                    Toast.makeText(getApplicationContext(), "内容为空，无法确定", Toast.LENGTH_SHORT).show();
                else {
                    str = Saying_content.split("\\s+"); //分割一个或者多个空格
                    if(str.length == 1){
                        queryNum1();
                    }else if(str.length == 2){
                        queryNum2();
                    }
                }
            }
        });
    }

    // 查询关键字个数就一个的情况
    public void queryNum1(){
        BmobQuery<descAnalyse> query = new BmobQuery<>();
        query.addWhereEqualTo("symptom",str[0]);
        query.findObjects(new FindListener<descAnalyse>() {
            @Override
            public void done(List<descAnalyse> list, BmobException e) {
                if(e == null){
                    if(list.size()>0){
                        for(descAnalyse d : list){
                            descResult.add(d.getKinds()); // kinds集
                            kindSet.add(d.getKind());     // kind 集
                        }

                        result.setVisibility(View.VISIBLE);
                        String temp = "";
                        for(String s: descResult){
                            temp += s+" ";
                        }

                        result.setText(temp);
                        showMessage();
                    }
                }else{
                    Toast.makeText(DescribeActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // 查询关键字个数两个的情况
    public void queryNum2(){
        List<BmobQuery<descAnalyse>> querySet = new ArrayList<>();
        for(String s:str){
            BmobQuery<descAnalyse> eq = new BmobQuery<descAnalyse>();
            eq.addWhereEqualTo("symptom",s);
            querySet.add(eq);
        }

        List<BmobQuery<descAnalyse>> queries = new ArrayList<BmobQuery<descAnalyse>>();
        for(BmobQuery<descAnalyse> eq1 : querySet){
            queries.add(eq1);
        }

        BmobQuery<descAnalyse> mainQuery = new BmobQuery<descAnalyse>();
        mainQuery.or(queries);
        mainQuery.findObjects(new FindListener<descAnalyse>() {
            @Override
            public void done(List<descAnalyse> object, BmobException e) {
                if(e==null){
                    if(object.size()>0){
                        for(descAnalyse d : object){
                            descResult.add(d.getKinds()); // kinds集
                            kindSet.add(d.getKind());     // kind 集
                        }

                        result.setVisibility(View.VISIBLE);
                        String temp = "";
                        for(String s: descResult){
                            temp += s+" ";
                        }

                        result.setText(temp);
                        showMessage();
                    }
                }else{
                    Toast.makeText(DescribeActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // 显示疾病详细信息
    public void showMessage(){
            for(String s : kindSet){
                if(s.equals("龋病")){
                    kind1.setVisibility(View.VISIBLE);
                }else if(s.equals("牙髓炎")){
                    kind2.setVisibility(View.VISIBLE);
                }else{
                    kind3.setVisibility(View.VISIBLE);
                }
            }
            onKindClickEvent();
    }

    // 返回判断是否输入提示
    public void createDialog() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.setMessage("内容无法保存，确定离开？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                }).create();
        alertDialog.show();
    }

    public void onKindClickEvent(){
        kind1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String kind = kind1.getText().toString();
                mScrollView.setVisibility(View.GONE);
                textRight.setVisibility(View.GONE);
                findData(kind);
            }
        });

        kind2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String kind = kind2.getText().toString();
                mScrollView.setVisibility(View.GONE);
                textRight.setVisibility(View.GONE);
                findData(kind);
            }
        });

        kind3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String kind = kind3.getText().toString();
                mScrollView.setVisibility(View.GONE);
                textRight.setVisibility(View.GONE);
                findData(kind);
            }
        });
    }

    public void findData(String kind){
        BmobQuery<message> query = new BmobQuery<>();
        query.addWhereEqualTo("kind",kind);
        String finalKind = kind;
        query.findObjects(new FindListener<message>() {
            @Override
            public void done(List<message> list, BmobException e) {
                if(e == null){
                    if(list.size()>0){
                        text1.setVisibility(View.VISIBLE);
                        text2.setVisibility(View.VISIBLE);
                        text3.setVisibility(View.VISIBLE);
                        text4.setVisibility(View.VISIBLE);
                        text5.setVisibility(View.VISIBLE);
                        text6.setVisibility(View.VISIBLE);
                        mScrollView.setVisibility(View.VISIBLE);
                        textRight.setVisibility(View.VISIBLE);

                        if(finalKind.equals("龋病")){
                            text3.setText("就医指征");
                            text4.setText("药物治疗");
                        }else if(finalKind.equals("牙髓炎")){
                            text3.setText("诱发因素");
                            text4.setText("药物治疗");
                        }else{
                            text3.setText("就医指数");
                            text4.setText("用药建议");
                        }
                        onItemClickEvent(list);
                    }
                }else{
                    Toast.makeText(DescribeActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void onItemClickEvent(List<message> list) {
        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textRight.setText(list.get(0).getMessage());
            }
        });
        text2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textRight.setText(list.get(0).getReason());
            }
        });
        text3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textRight.setText(list.get(0).getFactor());
            }
        });
        text4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textRight.setText(list.get(0).getDrug());
            }
        });
        text5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textRight.setText(list.get(0).getOperate());
            }
        });
        text6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textRight.setText(list.get(0).getPrecaution());
            }
        });
    }
}